--
-- Author: shunguo.chen
-- Date: 2018-04-03 15:34:32
--
-- PvpVSView	pvp宣战界面
--

local AvatarFactory = app:getClass("AvatarFactory")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpVSScene

function ClassRef:init()
	self:setBackGroundColorOpacity(255 * 0.9)
	self._ui.bg:setContentSize(display.size)

	local size = self._ui.bg_bom:getContentSize()
	size.width = display.width
	self._ui.bg_bom:setContentSize(size)
	self._ui.bg_top:setContentSize(size)
end

-- cell = UserBaseInfo/AvatarParams
-- infos = {{cell, ...}, {cell, ...}, {myName, enemyName}}
function ClassRef:refresh(infos)
	local myInfos = infos[1]
	local enemyInfos = infos[2]
	local names = infos[3]

	-- local myBase = myInfos[1]
	-- local enemyBase = enemyInfos[1]

	self._ui.name_1:setString(names[1])
	self._ui.name_2:setString(names[2])

	for i,info in ipairs(myInfos) do
		self:_laodHero(info, self._ui["Node_position_" .. i], false)
	end

	for i,info in ipairs(enemyInfos) do
		self:_laodHero(info, self._ui["Node_position_" .. (i+3)], true)
	end

	self:performWithDelay(function()
			executeFunc(self._onCloseView, self)
		end, 3)
end

function ClassRef:_laodHero(info, parent, isEnemy)
	if type(info) ~= "table" then return end

	AvatarFactory.preloadHero(info, function()
		if not tolua.isnull(self) then
			local hero = AvatarFactory.createHero(info)
			hero:setScaleX(isEnemy and 1 or -1)
			parent:addChild(hero)
			if hero:hasAnimation("angry") then
				hero:play("angry")
			else
				hero:play("mapidle")
			end
		end
	end)
end

return ClassRef
